# -*- coding: utf-8 -*-

import wx
import WbDateTime

__all__ = ['create']

def create(parent, file="", readonly=False):
    return WbEditPanel(parent, file, readonly)

[wxID_FRAME1TEXTCTRL1, wxID_FRAME1PANEL2, 
 wxID_FRAME1PANEL3, wxID_FRAME1PANEL4, 
 wxID_FRAME1STATICTEXT1, wxID_FRAME1STATICTEXT2, 
 wxID_FRAME1STATICTEXT3, wxID_FRAME1STATICTEXT4, 
] = [wx.NewId() for _init_ctrls in range(8)]

class WbEditPanel(wx.Panel):
    def _init_coll_flexGridSizer2_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.staticText2, 0, border=0, flag=0)
        parent.AddWindow(self.dtpc, 0, border=0, flag=0)
        parent.AddWindow(self.staticText3, 0, border=0, flag=0)
        parent.AddWindow(self.dtpm, 0, border=0, flag=0)
        parent.AddWindow(self.staticText4, 0, border=0, flag=0)
        parent.AddWindow(self.dtpa, 0, border=0, flag=0)

    def _init_coll_flexGridSizer2_Growables(self, parent):
        # generated method, don't edit

        parent.AddGrowableCol(0, 1)
        parent.AddGrowableCol(1, 2)

    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.staticText1, 0, border=0, flag=0)
        parent.AddWindow(self.textCtrl1, 0, border=0, flag=wx.EXPAND)

    def _init_coll_flexGridSizer1_Growables(self, parent):
        # generated method, don't edit

        parent.AddGrowableCol(0, 1)
        parent.AddGrowableCol(1, 2)

    def _init_coll_boxSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddSizer(self.flexGridSizer1, 0, border=10,
              flag=wx.EXPAND | wx.TOP)
        parent.AddWindow(self.staticLine1, 0, border=5,
              flag=wx.EXPAND | wx.BOTTOM | wx.TOP)
        parent.AddSizer(self.flexGridSizer2, 0, border=10,
              flag=wx.EXPAND | wx.BOTTOM)

    def _init_sizers(self):
        # generated method, don't edit
        self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)

        self.flexGridSizer1 = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=10)

        self.flexGridSizer2 = wx.FlexGridSizer(cols=2, hgap=0, rows=3, vgap=10)

        self._init_coll_boxSizer1_Items(self.boxSizer1)
        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_flexGridSizer2_Items(self.flexGridSizer2)
        self._init_coll_flexGridSizer2_Growables(self.flexGridSizer2)

        self.SetSizer(self.boxSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, prnt)
        self.SetMinSize((260, -1))

        self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
              label=_("FileName:"), name='staticText1', parent=self)

        self.textCtrl1 = wx.TextCtrl(self, wxID_FRAME1TEXTCTRL1,
              self.file, style=wx.TE_READONLY|wx.NO_BORDER)
        self.textCtrl1.SetBackgroundColour(self.GetBackgroundColour())

        self.staticLine1 = wx.StaticLine(self)

        self.staticText2 = wx.StaticText(id=wxID_FRAME1STATICTEXT2,
              label=_("Created:"), name='staticText2', parent=self)

        self.staticText3 = wx.StaticText(id=wxID_FRAME1STATICTEXT3,
              label=_("Modified:"), name='staticText3', parent=self)
            
        self.staticText4 = wx.StaticText(id=wxID_FRAME1STATICTEXT4,
              label=_("Accessed:"), name='staticText4', parent=self)

        self.dtpc = self.createDateTimePanel(self, self.ctime, not self.readonly)

        self.dtpm = self.createDateTimePanel(self, self.mtime, not self.readonly)

        self.dtpa = self.createDateTimePanel(self, self.atime, not self.readonly)

        self._init_sizers()

    def __init__(self, parent, file, readonly):
        self.file = file
        self.readonly = readonly
        if file:
            (self.ctime, self.mtime, self.atime) = getFileAttributes(file)
        else:
            self.ctime = self.mtime = self.atime = None
        self._init_ctrls(parent)
    
    def createDateTimePanel(self, parent, datetime, enable):
        date = time = ""
        if datetime:
            dt = datetime
            date = "%d-%d-%d" % (dt.GetYear(), dt.GetMonth()+1, dt.GetDay())
            time = "%d:%d:%d" % (dt.GetHour(), dt.GetMinute(), dt.GetSecond())
        return WbDateTime.create(parent, date, time, enable)
    
    def updateAttrs(self, file, readonly):
        def dt2datetime(dt):
            date = time = ""
            if dt:
                date = "%d-%d-%d" % (dt.Year, dt.Month+1, dt.Day)
                time = "%d:%d:%d" % (dt.Hour, dt.Minute, dt.Second)
            return (date, time)
        self.file = file
        self.readonly = readonly
        if file:
            (self.ctime, self.mtime, self.atime) = getFileAttributes(file)
        else:
            self.ctime = self.mtime = self.atime = None
        widgets = {
                self.dtpc: self.ctime,
                self.dtpm: self.mtime,
                self.dtpa: self.atime}
        self.textCtrl1.SetValue(self.file)
        for (dtp, dt) in widgets.items():
            (date, time) = dt2datetime(dt)
            dtp.update(date, time, not self.readonly)
    
def getFileAttributes(file_name):
    """
    file_name - File name
    Return [ctime, mtime, atime]
    """
    def getDateTime(id):
        datetime = wx.DateTime()
        datetime.SetTimeT(file_stats[id])
        return datetime
    import os, stat, time
    file_stats = os.stat(file_name)
    return [getDateTime(id) for id in (stat.ST_CTIME, stat.ST_MTIME, stat.ST_ATIME)]

class TestFrame(wx.Frame):
    def __init__(self, file):
        wx.Frame.__init__(self, None, -1)
        
        self.panel = wx.Panel(self)
        
        box = wx.StaticBox(self.panel, -1, "Browes File Attributes")
        
        p = create(self.panel, file, readonly=True)
        
        sizer = wx.StaticBoxSizer(box)
        sizer.Add(p, 1, wx.EXPAND|wx.ALL, 10)
        
        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.panel, 1, wx.EXPAND, 0)
        
        self.panel.SetSizer(sizer)
        self.SetSizer(self.sizer)
        self.Fit()
        
if __name__ == '__main__':
    from Main import installwxgettext
    installwxgettext()
    file = '1.txt'
    app = wx.PySimpleApp()
    TestFrame(file).Show()
    app.MainLoop()
    